export default class Handle {
  constructor(courseData, cartData, oAddBtns, observer) {
    this.courseData = courseData
    this.cartData = cartData
    this.oAddBtns = oAddBtns
    this.observer = observer

    this.init()
  }
  init () {
    this.observer.add(this.setState.bind(this))
    this.observer.add(this.setAddBtnState.bind(this))
  }
  setState (id, target, state) {
    console.log('setState',id,state);
    this.courseData = this.courseData.filter((item) => {
      if (item.id === id) {
        item.state = state

        if (state) {
          this.cartData.push(item)
        } else {
          this.cartData = this.cartData.filter((elem) => {
            if (elem.id === id) {
              return false
            }
            return true
          })
        }
      }
      return true
    })
  }

  /**
   * 设置添加购物车按钮状态
   * @param {*} id
   * @param {*} target
   * @param {*} state
   */
  setAddBtnState (id, target, state) {
    console.log('setAddBtnState',id,state);
    if (state) {
      target.innerText = '已添加'
      target.disabled = true
    } else {
      const thisAddBtn = this.oAddBtns[this.courseData.findIndex((item) => item.id == id)]
      thisAddBtn.innerText = '加入购物车'
      thisAddBtn.disabled = false
    }
  }
}
